package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.core.controller.AbstractCoreController;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.support.RequestContextUtils;

import java.util.Map;

/**
 * @description <p>自媒体图文内容信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }

    /**
     * 自媒体文章分页查询
     * @return
     */
    @PostMapping("/list")
    public PageResultVo<WmNews> findPage(@RequestBody WmNewsPageReqDto dto){
        return wmNewsService.findPage(dto);
    }

    /**
     * 发布文章
     * @param draft
     * @param dtoSave
     * @return
     */
    @PostMapping("/submit")
    public ResultVo submit(@RequestBody WmNewsDtoSave dtoSave ,boolean draft){
        //这里是取出状态参数校验的，修改nginx传参可以接收到
        /*Integer status = dtoSave.getStatus();
        if(status == 0){
            draft = true;
        }else if(status == 1){
            draft = false;
        }*/
        Integer wnNewsId = wmNewsService.submit(dtoSave,draft);
        return ResultVo.ok(wnNewsId);
    }

    @GetMapping("/one/{newsId}")
    public ResultVo<WmNews> findOneById(@PathVariable(value = "newsId") Integer newsId){
        WmNews wmNews = wmNewsService.getById(newsId);
        return ResultVo.ok(wmNews);
    }

    @GetMapping("/del_news/{newsId}")
    public ResultVo deleteById(@PathVariable(value = "newsId") Integer newsId){
        Boolean result = wmNewsService.deleteById(newsId);
        return ResultVo.ok(result);
    }

    @PostMapping("/down_or_up")
    public ResultVo downOrUp(@RequestBody Map<String,Integer> paramMap){
        wmNewsService.downOrUp(paramMap);
        return ResultVo.ok();
    }

}

